package com.webreport.webapp.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.webreport.exception.LoginException;
import com.webreport.exception.ManagerException;
import com.webreport.model.db.User;
import com.webreport.service.UserManager;
import com.webreport.utils.JsonUtils;
import com.webreport.webapp.bean.LoginBean;
import com.webreport.webapp.utils.SessionUtils;

@Controller("indexController")
public class IndexController extends BaseController {
	
	private UserManager userManager;
	
	@Autowired
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
		this.marshaller.setClassesToBeBound(LoginBean.class);
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/home/index")
	public ModelAndView index() {
		return new ModelAndView("home/index", "home", "");
	}
	
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = "/home/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, @ModelAttribute LoginBean login) {
		if (StringUtils.equals(login.getIslogin(), "Y")) {
			login.setErrormsg("");
			if (StringUtils.isEmpty(login.getUsername())) {
				login.setErrormsg("请输入用户名！！");
			}
			if (StringUtils.isEmpty(login.getPassword())) {
				login.setErrormsg("请输入登录密码！！");
			}
			try {
				User user = this.userManager.login(login.getUsername(), login.getPassword());
				login.setFullname(user.getFullname());
				login.setMultilogin(user.getMultilogin());
				login.setPassword("");
			} catch (ManagerException e) {
				login.setPassword("");
				login.setErrormsg("登录失败!" + e.getMessage());
			} catch (LoginException e) {
				login.setPassword("");
				login.setErrormsg(e.getMessage());
			}
			if (StringUtils.isEmpty(login.getErrormsg())) {
				try {
					SessionUtils.getInst().addSession(request, response, login);
					
				} catch (ManagerException e) {
					login.setErrormsg("登陆失败!" + e.getMessage());
				} catch (LoginException e) {
					login.setErrormsg(e.getMessage());
				}
				if (StringUtils.isEmpty(login.getErrormsg())) {
					if (!StringUtils.isEmpty(login.getRedirect())) {
						String redirect = login.getRedirect();
						login.setRedirect("");
						//redirect= StringUtils.substringAfterLast(redirect, "/service/");
						return new ModelAndView(new RedirectView(redirect), "login", login);
					} else {
						return index();
					}
				}
			}
		}
		return new ModelAndView("home/login", "login", login);
	}
	
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }, value = "/home/logoff")
	public ModelAndView logoff(HttpServletRequest request, HttpServletResponse response) throws ManagerException {
		SessionUtils.getInst().removeSession(request, response);
		return index();
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/home/loginuser")
	@ResponseBody
	public String loginUser(HttpServletRequest request) {
		LoginBean login = SessionUtils.getInst().getLogin(request);
		return JsonUtils.toJson(login);
	}
}
